package com.project.service.impl;

import com.project.bean.ShopBean;
import com.project.mapper.IProductMapper;
import com.project.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
public class ShopServiceImpl implements IShopService {
    @Autowired
    private IProductMapper mapper;

    @Override
    public void buy(int productId, Map<Integer, ShopBean> shopMap) {
        if(shopMap.containsKey(productId) == false){//商品不存在，新加元素
            //创建购物车对象
            ShopBean shopBean = new ShopBean();
            shopBean.setNum(1);
            shopBean.setProduct(mapper.findById(productId));

            shopMap.put(productId,shopBean);
        }
        else{
            //根据ID，找到对应购物车对象
           ShopBean  shopBean = shopMap.get(productId);
           shopBean.setNum(shopBean.getNum() + 1);
        }

    }

    @Override
    public void remove(int productId, Map<Integer, ShopBean> shopMap) {
        shopMap.remove(productId);
    }

    @Override
    public void updateNumber(int productId, Map<Integer, ShopBean> shopMap, int number) {
        ShopBean shopBean = shopMap.get(productId);
        shopBean.setNum(number);
    }
}
